package com.chuanyang.driverh.mainmy.fragment;


import android.Manifest;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chuanyang.driverh.BaseApplication;
import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseFragment;
import com.chuanyang.driverh.bean.IdentifyCarBean;
import com.chuanyang.driverh.mainmy.ShipIdentifyActivity;
import com.chuanyang.driverh.responseresult.Result;
import com.chuanyang.driverh.utils.BitmapUtil;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PermissoinUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.camera.DialogPhotoChooseView;
import com.chuanyang.driverh.utils.camera.DropDownList;
import com.chuanyang.driverh.utils.camera.SelectPictureListener;
import com.chuanyang.driverh.utils.camera.TakePictureListener;
import com.chuanyang.driverh.utils.eventbus.ImageEvent;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class IdentifyShipPhotoFragment extends BaseFragment {

    public static String[] imagepaths;
    private IdentifyCarBean identifyCarBean;
//    private MyViewPager pager;
    private ImageView iv_ship_project;
    private ImageView iv_ship_jianshu;
    private ImageView iv_ship_identity1;
    private ImageView iv_ship_identity2;
    private ImageView iv_ship_icon1;
    private ImageView iv_ship_icon2;
    private ImageView iv_ship_icon3;
//    private static final int CAMERA_READ_RITE_EXTERNAL_STORAGE = 101;
    private TextView tv_compete;

    @Override
    protected int getLayoutResId() {
        return R.layout.my_frag_identifypshiphoto;
    }

    @Override
    protected void initView(View view) {
        // addedCarInfoSub = identifyCarBean.getAddedCarInfoSub();
        iv_ship_project = (ImageView) rootView.findViewById(R.id.iv_ship_project);
        iv_ship_jianshu = (ImageView) rootView.findViewById(R.id.iv_ship_jianshu);
        //身份证
        iv_ship_identity1 = (ImageView) rootView.findViewById(R.id.iv_ship_identity1);
        iv_ship_identity2 = (ImageView) rootView.findViewById(R.id.iv_ship_identity2);


        //船只照片
        iv_ship_icon1 = (ImageView) rootView.findViewById(R.id.iv_ship_icon1);
        iv_ship_icon2 = (ImageView) rootView.findViewById(R.id.iv_ship_icon2);
        iv_ship_icon3 = (ImageView) rootView.findViewById(R.id.iv_ship_icon3);


      tv_compete = (TextView) rootView.findViewById(R.id.tv_identifyphoto_compete);
    }

    @Override
    protected void initData() {
        imagepaths = new String[7];
        if (getActivity() instanceof ShipIdentifyActivity) {
            identifyCarBean = ((ShipIdentifyActivity) getActivity()).getIdentifyCarBean();

        } else {
//            identifyCarBean = ((ShipPersonIdentifyActivity) getActivity()).getIdentifyCarBean();

        }
    }

    @Override
    protected void initListener() {
        tv_compete.setOnClickListener(this);
        iv_ship_project.setOnClickListener(this);
        iv_ship_jianshu.setOnClickListener(this);
        iv_ship_identity1.setOnClickListener(this);
        iv_ship_identity2.setOnClickListener(this);
        iv_ship_icon1.setOnClickListener(this);
        iv_ship_icon2.setOnClickListener(this);
        iv_ship_icon3.setOnClickListener(this);

    }
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_identifyphoto_compete:
                if (checkAll()) {
                    setEntity();
                    shipIdentify();
                }
                break;
            case R.id.iv_ship_project:
                ( (ShipIdentifyActivity)  getActivity()).setCurrentIndex(0);
                checkPermission();
                break;
            case R.id.iv_ship_jianshu:
                ( (ShipIdentifyActivity)  getActivity()).setCurrentIndex(1);
                checkPermission();
                break;
            case R.id.iv_ship_identity1:
                ( (ShipIdentifyActivity)  getActivity()).setCurrentIndex(2);
                checkPermission();
                break;
            case R.id.iv_ship_identity2:
                ( (ShipIdentifyActivity)  getActivity()).setCurrentIndex(3);
                checkPermission();
                break;
            case R.id.iv_ship_icon1:
                ( (ShipIdentifyActivity)  getActivity()).setCurrentIndex(4);
                checkPermission();
                break;
            case R.id.iv_ship_icon2:
                ( (ShipIdentifyActivity)  getActivity()).setCurrentIndex(5);
                checkPermission();
                break;
            case R.id.iv_ship_icon3:
                ( (ShipIdentifyActivity)  getActivity()).setCurrentIndex(6);
                checkPermission();
                break;
        }
    }

    private void checkPermission() {
        String[] perms = {Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
        if (PermissoinUtil.checkAndRequestPermissions(getActivity(),perms)){
            DialogPhotoChooseView mChooseView = new DialogPhotoChooseView(getActivity()).setCameraListener(new TakePictureListener(getActivity()))
                    .setAlbumListener(new SelectPictureListener(getActivity()));
            DropDownList.showDialog(getActivity(), mChooseView);

        }else{
            PrintUtil.toast(getActivity(),"请给应用相应的权限");
        }
    }


    private void setEntity() {
        identifyCarBean.setShipItemPt(imagepaths[0]);
        identifyCarBean.setInsurancePt1(imagepaths[1]);
        identifyCarBean.setIdentityPt1(imagepaths[2]);
        identifyCarBean.setIdentityPt2(imagepaths[3]);
        identifyCarBean.setCarPt1(imagepaths[4]);
        identifyCarBean.setCarPt2(imagepaths[5]);
        identifyCarBean.setCarPt3(imagepaths[6]);

    }
    private boolean checkAll() {
        if (TextUtils.isEmpty(imagepaths[0])) {
           PrintUtil.toast(getActivity(),"请上传项目页图片");
            return false;
        } else if (TextUtils.isEmpty(imagepaths[1])) {
            PrintUtil.toast(getActivity(),"请上传船检证书图片");
            return false;
        } else if (TextUtils.isEmpty(imagepaths[2]) || TextUtils.isEmpty(imagepaths[3])) {
            PrintUtil.toast(getActivity(),"请上传身份证照片");
            return false;
        } else if (TextUtils.isEmpty(imagepaths[4]) || TextUtils.isEmpty(imagepaths[5]) || TextUtils.isEmpty(imagepaths[6])) {
            PrintUtil.toast(getActivity(),"请上传三张船只照片");
            return false;
        }

        return true;
    }
    public void onEventMainThread(ImageEvent event) {
        if (event != null) {
            switch (event.getImageIndex())
            {
                case 0:
                    BitmapUtil.showImgForNetPath( event.getImagePath(),iv_ship_project);
                    imagepaths[event.getImageIndex()] = event.getImagePath();
                    break;
                case 1:
                    BitmapUtil.showImgForNetPath( event.getImagePath(), iv_ship_jianshu);
                    imagepaths[event.getImageIndex()] = event.getImagePath();
                    break;
                case 2:
                    BitmapUtil.showImgForNetPath( event.getImagePath(), iv_ship_identity1);
                    imagepaths[event.getImageIndex()] = event.getImagePath();
                    break;
                case 3:
                    BitmapUtil.showImgForNetPath( event.getImagePath(), iv_ship_identity2);
                    imagepaths[event.getImageIndex()] = event.getImagePath();
                    break;
                case 4:
                    BitmapUtil.showImgForNetPath( event.getImagePath(), iv_ship_icon1);
                    imagepaths[event.getImageIndex()] = event.getImagePath();
                    break;
                case 5:
                    BitmapUtil.showImgForNetPath( event.getImagePath(),iv_ship_icon2);
                    imagepaths[event.getImageIndex()] = event.getImagePath();
                    break;
                case 6:
                    BitmapUtil.showImgForNetPath( event.getImagePath(),iv_ship_icon3);
                    imagepaths[event.getImageIndex()] = event.getImagePath();
                    break;

            }
        }
    }


    private void shipIdentify() {
        DialogUtil.showLoadingDialog(getActivity(),"认证...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("driverId", SharedPrefUtil.getUserId());//用户id
        params.put("driverType", "3");//类型 （车2，船3）
        params.put("shipName", identifyCarBean.getShipName());//船名
        params.put("carType", identifyCarBean.getCarType());//船只类型
        params.put("carStandard", identifyCarBean.getCarStandard());//船只规格
        params.put("capacity", identifyCarBean.getCapacity());//吨位

        params.put("actualCapacity", identifyCarBean.getActualCapacity());//实际吨位
        params.put("draft", identifyCarBean.getDraft());//吃水
        params.put("hatchNum", identifyCarBean.getHatchNum());//舱口数
        params.put("hatchVolume", identifyCarBean.getHatchVolume());//舱口容积
        params.put("hatchSize", identifyCarBean.getHatchSize());//舱口尺寸
        if (identifyCarBean.getCompanyId() != null) {
            params.put("companyId", identifyCarBean.getCompanyId()+"");//公司id
        }
        params.put("plateNumber", identifyCarBean.getPlateNumber());//助记号
        params.put("frameNumber", identifyCarBean.getFrameNumber());//mmis号
        params.put("driverState", "2");//认证状态
        params.put("shipCreateDate", identifyCarBean.getShipCreateDate());//船出厂日期
        params.put("driverIdentity", identifyCarBean.getDriverIdentity());//身份证 （船）
        params.put("driverContact", identifyCarBean.getDriverContact());//联系方式 （船）
        params.put("shipItemPt", identifyCarBean.getShipItemPt());//项目页
        params.put("insurancePt1", identifyCarBean.getInsurancePt1());//保险图片1 （船：船检证书）
        params.put("identityPt1", identifyCarBean.getIdentityPt1());
        params.put("identityPt2", identifyCarBean.getIdentityPt2());
        params.put("carPt1", identifyCarBean.getCarPt1());
        params.put("carPt2", identifyCarBean.getCarPt2());
        params.put("carPt3", identifyCarBean.getCarPt3());

        MyHttpUtil.addCarInfo(getActivity(),params, new MyCallback<Result>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
            }

            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    PrintUtil.toast(getActivity(),"认证信息上传成功，请耐心等候");
                    BaseApplication.getInstance().getDriverInfoFromNet();
                    getActivity().onBackPressed();
                }else{
                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });

    }

}
